/**
 * 旋转
 */

import * as THREE from '../threejs/build/three.module.js';
import { PointerLockControls } from '../threejs/js/controls/PointerLockControls.js';
import { Fly } from './Fly.js';
import { createMsg } from './Msg.js';

let msg = createMsg();

let minHeight = -125;

class Turn {

    constructor(viewPoints, objects, scene, camera, controls, fly, defaultView, dingArr) {

        this.viewPoints = viewPoints;
        this.objects = objects;
        this.scene = scene;
        this.camera = camera;
        this.controls = controls;
        this.defaultView = defaultView;
        this.dingArr = dingArr;
        this.pointLockControls = new PointerLockControls(camera, document.body);;

        this.moveForward = false;
        this.moveBackward = false;
        this.moveLeft = false;
        this.moveRight = false;
        this._turnLeft = false;
        this._turnRight = false;

        this.isAutoFly = false;
        this.playIndex = 1;
        this.isRomaing = false;

        this.fly = fly;

        this.euler = new THREE.Euler(0, 0, 0, 'YXZ');

        this.velocity = new THREE.Vector3();
        this.direction = new THREE.Vector3();

        this.pointLockControls.addEventListener('lock', () => {
            this.isRomaing = true;
        });

        this.pointLockControls.addEventListener('unlock', () => {
            this.isRomaing = false;
            this.controls.enabled = true;
            this.controls && controls.target.copy(this.controls.getTarget());
            this.controls.update();
        });
    }

    bindDom() {

        //#region 预置点位列表绑定数据
        this.viewPoints.name.forEach((item, index) => {
            $("#selectView").append('<li value="' + index + '">' + item + '</li>');
        });

        $('#selectView li').click(e => {
            $('#selectView').slideToggle(350);
            let index = $(e.currentTarget).val();
            $("#btn-xingqudian").removeClass("active");
            this.fly.moveCameraOnly(this.viewPoints.position[index], this.viewPoints.target[index]);
        });
        //#endregion

        //#region 点击弹出预置点位列表
        $('#btn-xingqudian').on('click', e => {
            $(e.currentTarget).toggleClass("active");
            $('#selectView').slideToggle(350);
        });
        //#endregion

        //#region 上下左右按钮
        $("#top").on('mousedown', () => {
            this.moveForward = true;
            this.focusButton('w');
        });
        $("#top").on('mouseup', () => {
            this.moveForward = false;
            this.unFocusButton();
        });

        $("#bottom").on('mousedown', () => {
            this.moveBackward = true;
            this.focusButton('s');
        });
        $("#bottom").on('mouseup', () => {
            this.moveBackward = false;
            this.unFocusButton();
        });

        $("#left").on('mousedown', () => {
            this.moveLeft = true;
            this.focusButton('a');
        });
        $("#left").on('mouseup', () => {
            this.moveLeft = false;
            this.unFocusButton();
        });

        $("#right").on('mousedown', () => {
            this.moveRight = true;
            this.focusButton('d');
        });
        $("#right").on('mouseup', () => {
            this.moveRight = false;
            this.unFocusButton();
        });
        //#endregion

        //#region 左转右转按钮
        $("#rotate-left").on('mousedown', () => {
            this._turnRight = true;
            this.focusButton('e');
        });
        $("#rotate-left").on('mouseup', () => {
            this._turnRight = false;
            this.unFocusButton();
        });

        $("#rotate-right").on('mousedown', () => {
            this._turnLeft = true;
            this.focusButton('q');
        });
        $("#rotate-right").on('mouseup', () => {
            this._turnLeft = false;
            this.unFocusButton();
        });
        //#endregion

        //#region home按钮
        $('#stop').on('click', e => {
            this.fly.moveCameraOnly(this.defaultView.position, this.defaultView.target, () => {
                $(e.currentTarget).blur();
            });
        });
        //#endregion

        //#region 顶层显示隐藏
        $('#btn-ding').on('click', e => {
            $(e.currentTarget).toggleClass("active");
            this.dingArr.map(ding => {
                ding.material.visible = !ding.material.visible;
            });
        });
        //#endregion

        //#region 360全景
        $('#btn-360').on('click', () => {
            msg.show("暂未实现");
        });
        //#endregion

        //#region 自动飞行
        $('#auto-play').on('click', () => {
            msg.show("暂未实现");
        });
        //#endregion

        //#region 全屏
        $('#btn-fullScreen').on('click', e => {
            $(e.currentTarget).toggleClass("active");
            let element = document.getElementsByTagName('body')[0];

            if ($(e.currentTarget).hasClass('active')) {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.msRequestFullscreen) {
                    element.msRequestFullscreen();
                } else if (element.webkitRequestFullscreen) {
                    element.webkitRequestFullScreen();
                }
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExiFullscreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();

                } else if (document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                }
            }
        });
        //#endregion

        //#region 碰撞检测
        $('#btn-pengzhuang').on('click', e => {
            msg.show("暂未实现");
            $(e.currentTarget).toggleClass("active");
            if (this.isRomaing) {
                this.isRomaing = false;
                this.controls.enabled = true;
                this.controls.target.copy(this.controls.getTarget());
                this.controls.update();
            } else {
                this.controls.update();
                this.controls.enabled = false;
                this.isRomaing = true;
            }
        });
        //#endregion

        //#region 点击弹出摄像机设备列表
        $('#btn-camera').on('click', () => {
            msg.show("暂未实现");
        });
        //#endregion

    }

    turnLeft() {
        this.euler.setFromQuaternion(this.camera.quaternion);
        this.euler.y += 0.01;
        this.camera.quaternion.setFromEuler(this.euler);
        this.controls.target.copy(this.controls.getTarget());
    }

    turnRight() {
        this.euler.setFromQuaternion(this.camera.quaternion);
        this.euler.y -= 0.01;
        this.camera.quaternion.setFromEuler(this.euler);
        this.controls.target.copy(this.controls.getTarget());
    }

    roamingUpdate() {
        let delta = 0.15;
        let deltaY = 0.03;
        this.velocity.x -= this.velocity.x * 10.0 * delta;
        this.velocity.z -= this.velocity.z * 10.0 * delta;

        this.direction.z = Number(this.moveForward) - Number(this.moveBackward);
        this.direction.x = Number(this.moveRight) - Number(this.moveLeft);
        this.direction.normalize();

        if (this.moveForward || this.moveBackward) {
            this.velocity.z -= this.direction.z * 400.0 * delta;
        }

        if (this.moveLeft || this.moveRight) {
            this.velocity.x -= this.direction.x * 400.0 * delta;
        }

        this.pointLockControls.moveRight(-this.velocity.x * delta);
        this.pointLockControls.moveForward(-this.velocity.z * delta);

        this.pointLockControls.getObject().position.y += (this.velocity.y * deltaY);

        if (this.pointLockControls.getObject().position.y < minHeight) {
            this.pointLockControls.getObject().position.y = minHeight;
        }

        this.velocity.x = 0;
        this.velocity.z = 0;
    }

    focusButton(type) {
        switch (type) {
            case 'w': $('#top').focus(); break;
            case 'a': $('#left').focus(); break;
            case 's': $('#bottom').focus(); break;
            case 'd': $('#right').focus(); break;
            case 'e': $('#rotate-left').focus(); break;
            case 'q': $('#rotate-right').focus(); break;
        }
    }

    unFocusButton(type) {
        if (type) {
            switch (type) {
                case 'w': $('#top').blur(); break;
                case 'a': $('#left').blur(); break;
                case 's': $('#bottom').blur(); break;
                case 'd': $('#right').blur(); break;
                case 'e': $('#rotate-left').blur(); break;
                case 'q': $('#rotate-right').blur(); break;
            }
        } else {
            $('button').blur();
        }
    }

    refresh() {
        if (this.moveForward || this.moveBackward || this.moveLeft || this.moveRight) {
            this.roamingUpdate();
        }
        if (this._turnLeft) {
            this.turnLeft();
        }
        if (this._turnRight) {
            this.turnRight();
        }
        if (this.moveForward || this.moveBackward || this.moveLeft || this.moveRight) {
            this.controls.target.copy(this.controls.getTarget());
        }
    }
}

export { Turn }
